<?php


namespace App\Models\StoreCenter\Models;

use App\Models\ProductCenter\Models\SkuModel;
use App\Models\ProductCenter\Models\SpuModel;
use App\Models\StoreCenter\StoreCenterModel;

class CartModel extends StoreCenterModel
{
    public $table = 'cart';
    public $fillable = [
        'spu', //varchar(64) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'spu',
        'sku', //varchar(64) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'sku',
        'attribute', //varchar(64) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '属性',
        'sku_info', //json DEFAULT NULL COMMENT '商品详情',
        'amount', //int NOT NULL DEFAULT '0' COMMENT '数量',
        'price', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '原价',
        'actual_fee', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '特价',
        'discount', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '折扣价',
        'summary', //decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '小计',
        'shipping_fee', //decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '运费',
//        'create_time', //datetime DEFAULT NULL COMMENT '创建时间',
//        'update_time', //datetime DEFAULT NULL COMMENT '更新时间',
        'create_user', //varchar(64) COLLATE utf8mb4_general_ci NOT NULL COMMENT '创建者',
        'create_user_uuid', //varchar(32) COLLATE utf8mb4_general_ci NOT NULL COMMENT '创建者uuid',
    ];
    public $casts = [
      'sku_info' => 'json'
    ];

    public function skuInfo1()
    {
        return $this->belongsTo(SkuModel::class, 'sku', 'sku')->withDefault();
    }

    public function spuInfo()
    {
        return $this->belongsTo(SpuModel::class, 'spu', 'spu')->withDefault();
    }

    public function getCartList($where)
    {
        return $this->handleCondition($where)
            ->with([
                'skuInfo1:id,spu,sku,name,image_url,price,newest_price,stock',
                'spuInfo:id,spu,name,en_name,image_url',
            ])
            ->orderBy('create_time', 'desc')
            ->paginate(config('pageSize'));
    }
}
